package minov.parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import minov.parser.utils.StringUtils;

/**
 * This class is for arguments that have text values as their parameters.
 *
 */
public class TextArgument extends MultipleValueArgument<String> {

   /**
    * Constructs argument that parses text values.<br>
    * Example:
    * 
    * <pre>
    * intArgument = new IntegerArgument(&quot;t&quot;, &quot;timetolive&quot;, &quot;^[xyz]+&quot; ,null)
    * </pre>
    * 
    * @param [in] shortName
    *           argument shortcut
    * @param [in] fullName
    *           full name of argument
    * @param [in] validationRegexp
    *           regexp to validate the input. If the regexp is null, all text inputs are considered valid.
    * @param [in] defaultValues
    *           default value/values, if no argument was parsed
    */
   public TextArgument(String shortName, String fullName,
         String validationRegexp, String... defaultValues) {
      super(shortName, fullName, defaultValues);

      // create pattern from the entered regexp
      if (StringUtils.isEmptyString(validationRegexp) == false) {
         _validationRegexp = Pattern.compile(validationRegexp);
      }
   }

   private Pattern _validationRegexp = null;

   /**
    * Function for processing one argument. Called by the parser.
    * @param [in] text value to be processed
    * @return processed value
    */
   @Override
   public String parseOneArgument(String text) throws Exception {
      return text;
   }

   /*
    * format string for validation result
    */
   private static final String BAD_TEXT_FORMAT_STRING =
         "Argument %s failed regexp validation.";

   /**
    * Function for validating one argument value.
    * @param [in] value value to be validated
    * @return ValidationResult object with the result
    */
   @Override
   public ValidationResult validateValue(String value) {

      if (_validationRegexp != null) {
         Matcher matcher = _validationRegexp.matcher(value);
         if (matcher.find() == false) {
            return ValidationResult.createInvalidResponse(String.format(
                  BAD_TEXT_FORMAT_STRING, getDefaultName()));
         }
      }
      
      return ValidationResult.createValidResponse();
   }

   /**
    * Function for returning specific array type for this class. 
    * @return array of the specific type
    */
   @Override
   String[] getGenericArray() {
      return new String[0];
   }
   
 

}
